#ifndef __COMPONENT_CLOTHING_H__
#define __COMPONENT_CLOTHING_H__

#include "Component.hpp"
#include "Game/IInventory.hpp"
#include "GameEnum.h"
#include <vector>
#include <mutex>
#include "Game/ClothingData.h"

namespace PocketSurvival
{
    struct ComponentVitalStats;
    struct ComponentBody;
    struct ComponentPlayer;

    struct ComponentClothing : public Component, public IInventory
    {
        std::mutex clothesMutex;
        std::vector<std::vector<int32_t>> clothes = {std::vector<int32_t>{}, std::vector<int32_t>{}, std::vector<int32_t>{}, std::vector<int32_t>{}};

        float steedMovementSpeedFactor;
        float insulation;
        double lastTotalElapsedGameTime;
        // ClothingSlot leastInsulatedSlot;

        ComponentVitalStats *componentVitalStats;
        ComponentBody *componentBody;
        ComponentPlayer *componentPlayer;


        virtual uint32_t slotsCount() override;
        virtual void setSlotValue(uint32_t slotIndex, int32_t cellValue, uint32_t valueCount) override;
        virtual int32_t getSlotValue(uint32_t slotIndex) override;
        virtual uint32_t getSlotCount(uint32_t slotIndex) override;
        virtual void onSlotChange(uint32_t slotIndex) override;
        virtual uint32_t getSlotCapacity(uint32_t slotIndex, int32_t cellValue) override;
        virtual uint32_t getSlotProcessCapacity(uint32_t slotIndex, int32_t cellValue) override;
        virtual void addSlotItems(uint32_t slotIndex, int32_t cellValue, uint32_t count) override;
        virtual bool addNetSlotItems(uint32_t slotIndex, int32_t cellValue, uint32_t count) override;
        virtual void processSlotItems(IInventory *sourceInventory, uint32_t sourceSlotIndex, uint32_t slotIndex, int32_t cellValue, uint32_t count, uint32_t processCount, int32_t &outProcessedValue, uint32_t &outProcessedCount) override;
        virtual uint32_t removeSlotItems(uint32_t slotIndex, uint32_t count) override;
        virtual uint32_t removeNetSlotItems(uint32_t slotIndex, uint32_t count) override;
        virtual void dropAllItems(const Vector3 &position) override;
        virtual void dropSlotItems(uint32_t slotIndex, const Vector3 &position, const Vector3 &velocity) override;

        // 考虑到多线程的问题，而且这里的数据也不会很大，所以使用拷贝的数据即可
        inline std::vector<int32_t> getClothes(ClothingSlot slot);

        void setClothes(ClothingSlot slot, const std::vector<int32_t> &theClothes);
        void updateAttribute();
        bool canWearClothing(int32_t value);
        float applyArmorProtection(float attackPower);

        virtual void update(float dt) override;
        virtual const std::string &getName() const override;
        static const std::string &GetName();
        virtual void load(const nlohmann::json &jsonData) override;
        virtual void save(nlohmann::json &jsonData) override;
        virtual void saveToMsgJson(nlohmann::json &jsonData) override;
    };

    inline std::vector<int32_t> ComponentClothing::getClothes(ClothingSlot slot)
    {
        std::lock_guard<std::mutex> lk(clothesMutex);
        return clothes[(uint8_t)slot];
    }

} // namespace PocketSurvival

#endif